unit RelatorioDM;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, DB, FileUtil, LResources, Forms, Controls, Dialogs,
  ZDataset, LR_Class, LR_DBSet,
  lr_e_pdf;

type

  { TRelatorioData }

  TRelatorioData = class(TDataModule)
    DetalheDatasource: TDatasource;
    PrincipalPDFExport: TfrTNPDFExport;
    ExportDialog: TSaveDialog;
    RelatorioDataSource: TDatasource;
    DetalheRDBDataSet: TfrDBDataSet;
    RelatorioRDBDataSet: TfrDBDataSet;
    PrincipalReport: TfrReport;
    DetalheZQueryareia: TFloatField;
    DetalheZQueryargila: TFloatField;
    DetalheZQueryclassificacao: TStringField;
    DetalheZQueryid_amostra: TLongintField;
    DetalheZQueryid_analise: TLongintField;
    DetalheZQuerypeso_becker_1: TLongintField;
    DetalheZQuerypeso_becker_2: TLongintField;
    DetalheZQuerypeso_volume_pipetado_1: TLongintField;
    DetalheZQuerypeso_volume_pipetado_2: TLongintField;
    DetalheZQueryreferencia_cliente: TStringField;
    DetalheZQueryreferencia_laboratorio: TLongintField;
    DetalheZQuerysilte: TFloatField;
    RelatorioZQuery: TZReadOnlyQuery;
    DetalheZQuery: TZReadOnlyQuery;
    RelatorioZQuerybairro: TStringField;
    RelatorioZQuerycep: TStringField;
    RelatorioZQuerycrea: TStringField;
    RelatorioZQuerydata: TDateField;
    RelatorioZQueryemail: TStringField;
    RelatorioZQueryendereco_laboratorio: TStringField;
    RelatorioZQueryendereco_produtor: TStringField;
    RelatorioZQueryformacao: TStringField;
    RelatorioZQueryid_analise: TLongintField;
    RelatorioZQuerymunicipio_laboratorio: TStringField;
    RelatorioZQuerymunicipio_produtor: TStringField;
    RelatorioZQuerynome_laboratorio: TStringField;
    RelatorioZQuerynome_produtor: TStringField;
    RelatorioZQuerynome_tecnico: TStringField;
    RelatorioZQuerynumero_registro: TLongintField;
    RelatorioZQuerytelefone: TStringField;
    RelatorioZQueryuf_laboratorio: TStringField;
    RelatorioZQueryuf_produtor: TStringField;
    RelatorioZQueryvalor: TFloatField;
    procedure GeraRelatorio();
    procedure MostraRelatorio();
    procedure SalvaPDF();
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  RelatorioData: TRelatorioData;

implementation

uses ConnectionDM, AnaliseDM;

{ TRelatorioData }

procedure TRelatorioData.GeraRelatorio();
var
  Location: string;
begin
  Location := ExtractFilePath(Application.ExeName) + 'relatorio' + PathDelim;
  with RelatorioZQuery do begin
    Close();
    SQL.LoadFromFile(Location + 'laudo.sql');
    ParamByName('id_analise').AsInteger := AnaliseData.GetCurrentIdAnalise();
  end;
  RelatorioData.DetalheZQuery.Active := True;
  RelatorioData.RelatorioZQuery.Active := True;
  RelatorioData.PrincipalReport.LoadFromFile(Location + 'laudo.lrf');
end;

procedure TRelatorioData.MostraRelatorio();
begin
  GeraRelatorio();
  RelatorioData.PrincipalReport.ShowReport();
end;

procedure TRelatorioData.SalvaPDF();
begin
  ExportDialog.InitialDir := GetUserDir();
  if (ExportDialog.Execute) then begin
    GeraRelatorio();
    PrincipalReport.PrepareReport;
    PrincipalReport.ExportTo(TfrTNPDFExportFilter, ExportDialog.FileName);
  end;
end;

//! Guardar um QueryConditions nos frames para usar nos relatórios ou atualizar as consultas

{ TRelatorioData }

initialization
  {$I relatoriodm.lrs}

end.
